//
//  DYDataBaseTool.m
//  Craft
//
//  Created by BetaTheta on 16/3/19.
//  Copyright © 2016年 BetaTheta. All rights reserved.
//

#import "DYDataBaseTool.h"

@implementation DYDataBaseTool
+ (DYDataBaseTool *)shareDYDataBaseTool{
    
    static DYDataBaseTool *dataBaseTool = nil;
    static dispatch_once_t oneToKen;
    dispatch_once(&oneToKen, ^{
        
        dataBaseTool = [[DYDataBaseTool alloc] init];
        [dataBaseTool openDB];
        [dataBaseTool createCourseTable];
    });
    
    return dataBaseTool;
}

// ------- 打开数据库 --------//
- (void)openDB{
    
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *sandBoxPath = sandBox[0];
    
    NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"Course.sqlite"];
    NSLog(@"%@",documentPath);
    int result = sqlite3_open([documentPath UTF8String], &dbPoint);
    if (result == SQLITE_OK) {
        NSLog(@"数据库打开成功");
    }else{
        NSLog(@"数据库打开失败");
    }
    // 数据库一般放在Documents下
    // 后缀:db/sqlite
   
}

// --------- 创建一个收藏的表格 -------------//
- (void)createCourseTable
{
    
    NSString *sqliteStr = @"create table if not exists course (number integer primary key autoincrement,hand_id text, subject text, host_pic text, user_name text)";
    
    int result = sqlite3_exec(dbPoint, [sqliteStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"收藏表格创建成功");
    }else{
        NSLog(@"收藏表格创建失败");
    }
}


// ------- 判断是否保存 ---------------------//
- (DYIsSaveDataInTable)isSaveDataInTableWithHand_id:(NSString *)hand_id type:(DYDataType)type
{
    switch (type) {
        case DYCollect:
        {
            NSString *sqliteStr = [NSString stringWithFormat:@"select * from course where hand_id = '%@'",hand_id];
            
            // 跟随指针
            sqlite3_stmt *stmt = nil;
            
            int result = sqlite3_prepare_v2(dbPoint, [sqliteStr UTF8String], -1, &stmt, nil);
            
            if (result == SQLITE_OK) {
                NSLog(@"查询教程表格成功");
                
                if (sqlite3_step(stmt) == SQLITE_ROW) {
                    sqlite3_finalize(stmt);
                    NSLog(@"教程已在表中");
                    return DYInTabel;
                }
                sqlite3_finalize(stmt);
                NSLog(@"教程没有在表中");
                return DYNotInTable;
            }else{
                sqlite3_finalize(stmt);
                NSLog(@"查询教程表格失败啊!");
                return DYSelectError;
            }
        }
            break;
        default:
            break;
    }
    
}


#pragma mark - 增
// ------------- 添加 收藏 ---------------//
- (void)insertDataWithCourseModel:(DYCollect_Model *)model
{
    
    NSString *sqliteStr =[NSString stringWithFormat:@"insert into course (hand_id, subject, host_pic, user_name) values('%@','%@', '%@','%@')",model.hand_id, model.subject, model.host_pic, model.user_name];
    
    int result = sqlite3_exec(dbPoint, [sqliteStr UTF8String], nil, nil, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"添加教程成功");
    }else{
        NSLog(@"添加教程失败");
    }
    
}

#pragma mark - 删
// ------------------  删除 --------------------//
- (void)delegateDataWithHand_id:(NSString *)hand_id type:(DYDataType)type
{
    switch (type) {
        case DYCollect:
        {
            NSString *sqlStr = [NSString stringWithFormat:@"delete from course where hand_id = '%@'", hand_id];
            
            int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
            if (result == SQLITE_OK) {
                NSLog(@"删除教程成功!");
            }else{
                NSLog(@"删除教程失败!");
            }
            
        }
            break;
            
        default:
        break;
    }
    
}

#pragma mark - 改
// ------------------- 更新 --------------------//
- (void)upDataWithHand_id:(NSString *)hand_id Num:(NSString *)num type:(DYDataType)type
{
    switch (type) {
        case DYCollect:
        {
            NSString *sqlStr = [NSString stringWithFormat:@"update course set collect= '%@' where hand_id = '%@'",num, hand_id];
            int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
            if (result == SQLITE_OK) {
                NSLog(@"更新教程成功");
            }else{
                NSLog(@"更新教程失败! 原因:%d",result);
            }
            
        }
            break;
            
        default:
            break;
    }
    
}

#pragma mark - 查
// -------------- 遍历所有取出 -------------------//
- (NSMutableArray *)selectAllWithtype:(DYDataType)type{
    
    switch (type) {
        case DYCollect:
        {
            NSString *sqlStr = @"select * from course";
            
            NSMutableArray *courseArr = [NSMutableArray array];
            
            sqlite3_stmt *stmt = nil;
            
            int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
            
            if (result == SQLITE_OK) {
                NSLog(@"查询收藏所有表格成功!");
                while (sqlite3_step(stmt) == SQLITE_ROW) {
                    const unsigned char *hand_id = sqlite3_column_text(stmt, 1);
                    const unsigned char *subject = sqlite3_column_text(stmt, 2);
                    const unsigned char *host_pic = sqlite3_column_text(stmt, 3);
                    const unsigned char *user_name = sqlite3_column_text(stmt, 4);
//                    const unsigned char *collect = sqlite3_column_text(stmt, 5);
                    
                    
                    NSString *courseHand_id = [NSString stringWithUTF8String:(const char *)hand_id];
                    NSString *courseSubject = [NSString stringWithUTF8String:(const char *)subject];
                    NSString *courseHost_pic = [NSString stringWithUTF8String:(const char *)host_pic];
                    NSString *courseUser_name = [NSString stringWithUTF8String:(const char *)user_name];
//                    NSString *courseCollect = [NSString stringWithUTF8String:(const char *)collect];
                   
                    
                    DYCollect_Model *model = [[DYCollect_Model alloc] init];
                    model.hand_id = courseHand_id;
                    model.subject = courseSubject;
                    model.host_pic = courseHost_pic;
                    model.user_name = courseUser_name;
//                    model.collect = courseCollect;
                    
                    
                    [courseArr addObject:model];
                }
                //
            }else{
                sqlite3_finalize(stmt);
                NSLog(@"查询教程失败");
            }
            sqlite3_finalize(stmt);
            return courseArr;
        }
            
        default:
            break;
    }
    
}

- (void)deleteAllCourse
{
    NSString *sqlStr = [NSString stringWithFormat:@"delete  from course"];
    
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除全部教程成功!");
    }else{
        NSLog(@"删除全部教程失败!");
    }
}

@end
